package org.example.workbench.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.User;
import org.example.workbench.domain.ClueRemark;
import org.example.workbench.service.ClueRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/clueRemark")
public class clueRemarkController {
    @Autowired
    private ClueRemarkService clueRemarkService;

    @RequestMapping("/deleteRemarkByClueId")
    public @ResponseBody Object deleteRemarkByClueId(String id){
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = clueRemarkService.deleteClueRemarkById(id);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后.......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后.......");
        }
        return errorInfo;
    }
    @RequestMapping("/updateClueRemarkByMap")
    public @ResponseBody Object updateClueRemarkByMap(@RequestParam Map<String,Object> map,HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        String dateTime = DateFormatUtils.formatDateTime(new Date());
        map.put("editTime",dateTime);
        map.put("editBy",user.getId());
        map.put("editFlag",Constant.EDIT_FLAG_YES);
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = clueRemarkService.updateClueRemarkByMap(map);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后.......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后.......");
        }
        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("editTime",dateTime);
        hashMap.put("errorInfo",errorInfo);
        return hashMap;
    }
    @RequestMapping("/selectOneClueRemarkById")
    public @ResponseBody Object selectOneClueRemarkById(String id){
        return clueRemarkService.selectOneClueRemarkById(id);
    }
    @RequestMapping("/saveClueRemark")
    public @ResponseBody Object saveClueRemark(ClueRemark clueRemark, HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        clueRemark.setId(UUIDUtils.getUUID());
        //插入数据库使用创建者的id
        clueRemark.setCreateBy(user.getId());
        clueRemark.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        clueRemark.setEditFlag(Constant.EDIT_FLAG_NO);
        clueRemarkService.insertOneClueRemarkByClueRemark(clueRemark);
        return clueRemark;
    }
}
